1292F - Nora's Toy Boxes - CodeForces Solution


bitmasks combinatorics dp *3500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
 using namespace std;
typedef long long ll;
const int N=65, K=16, mod=1000000007;
 ll n, m, k, u, v, x, y, t, a, b, ans=1, ted;
int A[N], B[N], src[N], vec[N], adj[N];
ll dp[N][1<<K], C[N][N];
 void dfs(int v){
 A[v]=0;
 if (B[v]) vec[m++]=v;
 else src[k++]=v;
 for (int u=1; u<N; u++) if (A[u] && (u%v==0 || v%u==0)) dfs(u);
}
 int main(){
 for (int i=0; i<N; i++){
  C[i][0]=C[i][i]=1;
  for (int j=1; j<i; j++) C[i][j]=(C[i-1][j] + C[i-1][j-1])%mod;
 }
 cin>>n;
 while (n--) cin>>x, A[x]=1;
 for (int i=1; i<N; i++) if (A[i]) for (int j=i+i; j<N; j+=i) B[j]=1;
 for (int i=1; i<N; i++) if (A[i]){
  m=k=0;
  dfs(i);
  if (!m) continue ;
  m--;
  for (int t=0; t<=m; t++) for (int mask=0; mask<(1<<k); mask++) dp[t][mask]=0;
  for (int j=0; j<=m; j++){
   adj[j]=0;
   for (int a=0; a<k; a++) if (vec[j]%src[a]==0) adj[j]|=(1<<a);
   dp[0][adj[j]]++;
  }
  for (int j=0; j<m; j++) for (int mask=1; mask<(1<<k); mask++){
   dp[j][mask]%=mod;
   for (int x=0; x<=m; x++) if (mask&adj[x]) dp[j+1][mask|adj[x]]+=dp[j][mask];
   dp[j+1][mask]-=dp[j][mask]*(j+1);
  }
  ans=ans*dp[m][(1<<k)-1]%mod*C[ted+=m][m]%mod;
 }
 cout<<ans<<"\n";
  return 0;
}
 


Comments

Submit
0 Comments
More Questions

1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom